<!-- eslint-disable no-undef -->
<script setup lang="ts">
import { OrderType } from '@/enums'
import { uploadImg } from '@/services/consult'
import type { Image } from '@/types/consult'
import { showToast } from 'vant'
import type { UploaderAfterRead } from 'vant/lib/uploader/types'

defineProps<{
  status: OrderType
}>()
const emits = defineEmits<{
  (e: 'sendMsg', v: string): void
  (e: 'sendImg', v: Image): void
}>()
const msg = ref('')
const sendMsg = () => {
  if (!msg.value) return showToast('请输入要咨询的信息')
  // 子传父 将数据交给父组件, 让父组件 发送消息给socket
  emits('sendMsg', msg.value)
  // 输入框清空
  msg.value = ''
}
// 读取图片完毕后 发送请求获取到上传的图片信息, 通知父组件
const afterRead: UploaderAfterRead = async (item) => {
  if (Array.isArray(item)) return
  if (!item.file) return
  // 处理 FormData
  const fm = new FormData()
  fm.append('file', item.file)
  const res = await uploadImg(fm)
  console.log(res)
  emits('sendImg', res.data)
}
</script>

<template>
  <div class="room-action">
    <van-field
      type="text"
      class="input"
      :border="false"
      placeholder="问医生"
      autocomplete="off"
      :disabled="status !== OrderType.ConsultChat"
      v-model.trim="msg"
      @keyup.enter="sendMsg"
    ></van-field>
    <van-uploader
      :preview-image="false"
      :disabled="status !== OrderType.ConsultChat"
      :after-read="afterRead"
    >
      <cp-icon name="consult-img" />
    </van-uploader>
  </div>
</template>

<style lang="scss" scoped>
.room-action {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 60px;
  background-color: #fff;
  display: flex;
  align-items: center;
  padding: 0 16px;
  z-index: 1;
  box-sizing: border-box;
  .input {
    background-color: var(--cp-bg);
    border: none;
    border-radius: 25px;
    margin-right: 10px;
    padding: 8px 15px;
  }
  .cp-icon {
    width: 24px;
    height: 24px;
  }
}
</style>
